package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.report.ReportUtil;
import com.ruoyi.system.domain.SysDiagnosisDetail;
import com.ruoyi.system.domain.SysNaire;
import com.ruoyi.system.mapper.SysQuestionRecordMapper;
import com.ruoyi.system.service.*;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysDiagnosisReportMapper;
import com.ruoyi.system.domain.SysDiagnosisReport;
import springfox.documentation.spring.web.json.Json;
import utils.ReportCreater;

/**
 * 诊断报告Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-23
 */
@Service
public class SysDiagnosisReportServiceImpl implements ISysDiagnosisReportService 
{
    @Autowired
    private ISysDiagnosisDetailService sysDiagnosisDetailService;

    @Autowired
    private SysDiagnosisReportMapper sysDiagnosisReportMapper;

    @Autowired
    private ISysQuestionService sysQuestionService;

    @Autowired
    private ISysNaireService sysNaireService;

    @Autowired
    private ISysQuestionDomainService domainService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysQuestionRecordService sysQuestionRecordService;

    /**
     * 查询诊断报告
     * 
     * @param reportId 诊断报告主键
     * @return 诊断报告
     */
    @Override
    public SysDiagnosisReport selectSysDiagnosisReportByReportId(Long reportId)
    {
        return sysDiagnosisReportMapper.selectSysDiagnosisReportByReportId(reportId);
    }

    @Override
    public int generateSysDiagnosisReportByReportId(SysDiagnosisReport sysDiagnosisReport) {
        //获得诊断结果
        SysDiagnosisDetail dquery=new SysDiagnosisDetail();
        dquery.setCompanyId(sysDiagnosisReport.getCompanyId());
        dquery.setNaireId(sysDiagnosisReport.getNaireId());
        List<SysDiagnosisDetail> sysDiagnosisDetails=sysDiagnosisDetailService.selectSysDiagnosisDetailList(dquery);


        //获得问题
        SysQuestion qquery=new SysQuestion();
        List<SysQuestion> sysQuestions = sysQuestionService.selectSysQuestionList(qquery);

        //根据问卷对问题进行过滤
        SysNaire naire = sysNaireService.selectSysNaireByNaireId(sysDiagnosisReport.getNaireId());
        List<String> quesetionIds = Arrays.asList(naire.getQuestions().substring(1, naire.getQuestions().length() - 1).split(","));


        List<SysQuestion> result = new ArrayList<>();
        for (SysQuestion sysQuestion : sysQuestions) {
            if (quesetionIds.contains(sysQuestion.getQuestionId() + ""))
                result.add(result.size(), sysQuestion);
        }

        SysQuestionRecord rquery=new SysQuestionRecord();
        rquery.setCompanyId(sysDiagnosisReport.getCompanyId());
        rquery.setNaireId(sysDiagnosisReport.getNaireId());
        List<SysQuestionRecord> records=sysQuestionRecordService.selectSysQuestionRecordList(rquery);



        //获得问题领域
        List<SysQuestionDomain> domains = domainService.selectQuestionDomainList(new SysQuestionDomain());

        HashMap<Long,String> domainNameMap=new HashMap<>();
        for (SysQuestionDomain domain:domains){
            domainNameMap.put(domain.getDomainId(),domain.getDomainName());
        }
        List<TreeSelect> domainTrees = domainService.buildQuestionDomainTreeSelect(domains);

        String rptmpl="/reporttmpl/tpml-v2.0.docx";
        SysDept company = deptService.selectDeptById(sysDiagnosisReport.getCompanyId());

        ReportCreater creater=new ReportCreater(company.getDeptId(),company.getDeptName(),sysDiagnosisReport.getNaireId(),domainTrees,rptmpl,sysDiagnosisDetails,result,records);

        String reportpath=creater.generReport();

        sysDiagnosisReport.setReport(reportpath.replaceAll(RuoYiConfig.getProfile(),"/profile/"));
        sysDiagnosisReport.setType("1");
        int row=insertSysDiagnosisReport(sysDiagnosisReport);
        return row;
    }




    /**
     * 查询诊断报告列表
     * 
     * @param sysDiagnosisReport 诊断报告
     * @return 诊断报告
     */
    @Override
    public List<SysDiagnosisReport> selectSysDiagnosisReportList(SysDiagnosisReport sysDiagnosisReport)
    {

        return sysDiagnosisReportMapper.selectSysDiagnosisReportList(sysDiagnosisReport);
    }

    /**
     * 新增诊断报告
     * 
     * @param sysDiagnosisReport 诊断报告
     * @return 结果
     */
    @Override
    public int insertSysDiagnosisReport(SysDiagnosisReport sysDiagnosisReport)
    {
        sysDiagnosisReport.setCreateTime(DateUtils.getNowDate());
        return sysDiagnosisReportMapper.insertSysDiagnosisReport(sysDiagnosisReport);
    }

    /**
     * 修改诊断报告
     * 
     * @param sysDiagnosisReport 诊断报告
     * @return 结果
     */
    @Override
    public int updateSysDiagnosisReport(SysDiagnosisReport sysDiagnosisReport)
    {
        return sysDiagnosisReportMapper.updateSysDiagnosisReport(sysDiagnosisReport);
    }

    /**
     * 批量删除诊断报告
     * 
     * @param reportIds 需要删除的诊断报告主键
     * @return 结果
     */
    @Override
    public int deleteSysDiagnosisReportByReportIds(Long[] reportIds)
    {
        return sysDiagnosisReportMapper.deleteSysDiagnosisReportByReportIds(reportIds);
    }

    /**
     * 删除诊断报告信息
     * 
     * @param reportId 诊断报告主键
     * @return 结果
     */
    @Override
    public int deleteSysDiagnosisReportByReportId(Long reportId)
    {
        return sysDiagnosisReportMapper.deleteSysDiagnosisReportByReportId(reportId);
    }
}
